package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class OmnistrainLogoState extends FlxState
	{
		private var _omnistrainSprite:FlxSprite;
		
		//used a counter to act as a timer to transition
		private var _counter:Number = 0;
		private const _timeLimit:Number = 3.7;
		private const _fadeTime:Number = 1.0;
		private var _fadeSprite:FlxSprite;
		private var _quakePhase:uint = 1;
		private var _startedQuake:Boolean = false;
		
		override public function create():void
		{
			FlxG.mouse.show();
			
			_omnistrainSprite = new FlxSprite(0, 0, ImageFiles.omnistrainLogoImage);
			_omnistrainSprite.antialiasing = true;
			add(_omnistrainSprite);
			
			_fadeSprite = new FlxSprite().createGraphic(640, 480, 0xff000000);
			_fadeSprite.alpha = 0;
			add(_fadeSprite);
			
			FlxG.playMusic(SoundFiles.themeMusic);
			FlxG.quake.start(0.005, _timeLimit / 3);
			_startedQuake = true;
		}
		
		override public function update():void 
		{
			_counter += FlxG.elapsed;
			
			_omnistrainSprite.angle = -_counter / 3;
			
			if (_counter > _timeLimit / 3)
			{
				if (_counter > _timeLimit / 3 * 2)
				{
					_quakePhase = 3;
					_startedQuake = false;
				}
				else
				{
					_quakePhase = 2;
					_startedQuake = false;
				}
			}
			
			if (!_startedQuake)
			{
				switch (_quakePhase)
				{
					case 2:
					FlxG.quake.start(0.010, _timeLimit / 3);
					_startedQuake = true;
					break;
					case 3:
					FlxG.quake.start(0.020, _timeLimit / 3);
					_startedQuake = true;
					break;
				}
			}
			
			_fadeSprite.alpha = (_fadeTime - _counter);
			
			if (_timeLimit - _counter <= _fadeTime)
			{
				_fadeSprite.alpha = Math.abs(1 - ((_timeLimit - _counter) / _fadeTime));
			}
			
			if (_counter >= _timeLimit)
			{
				goToMainMenu();
			}
			
			super.update();
		}
		
		private function goToMainMenu():void
		{
			FlxG.state = new MainMenuState(true, false);
		}
		
	}

}